#include "CLMarkWidget.hpp"
#include "InfoWidget.hpp"
#include "BenchmarkWidget.hpp"
#include <QHBoxLayout>
#include <QLabel>

using namespace ui;

CLMarkWidget::CLMarkWidget(QWidget *parent) :
    QWidget(parent),
    infoWidgetHolder(new QHBoxLayout()),
    infoWidget(0),
    benchmarkWidgetHolder(new QHBoxLayout()),
    benchmarkWidget(0),
    platform(0),
    device(0) {
    QHBoxLayout *layout = new QHBoxLayout();
    setLayout(layout);
    layout->setMargin(0);
    layout->setSpacing(0);
    infoWidgetHolder->setMargin(0);
    infoWidgetHolder->setSpacing(0);
    benchmarkWidgetHolder->setMargin(0);
    benchmarkWidgetHolder->setSpacing(0);
    QWidget *infoWidget = new QWidget();
    infoWidget->setLayout(infoWidgetHolder);
    layout->addWidget(infoWidget);
    QWidget *benchmarkWidget = new QWidget();
    benchmarkWidget->setLayout(benchmarkWidgetHolder);
    layout->addWidget(benchmarkWidget);
}

void CLMarkWidget::setPlatform(const cl::Platform &platform) {
    this->platform = &platform;
}

void CLMarkWidget::setDevice(const cl::Device &device) {
    this->device = &device;
}

void CLMarkWidget::showInfo(void) {
    if(infoWidget) {
        infoWidgetHolder->removeWidget(infoWidget);
        delete infoWidget;
        infoWidget = 0;
    }
    infoWidget = new InfoWidget(*platform, *device);
    infoWidgetHolder->addWidget(infoWidget);
}

void CLMarkWidget::showBenchmark(void) {
    if(benchmarkWidget) {
        benchmarkWidgetHolder->removeWidget(benchmarkWidget);
        delete benchmarkWidget;
        benchmarkWidget = 0;
    }
    benchmarkWidget = new BenchmarkWidget(*device);
    benchmarkWidgetHolder->addWidget(benchmarkWidget);
    connect(benchmarkWidget, SIGNAL(isRunning(bool)),
            this,            SLOT(onIsRunning(bool)));
    benchmarkWidget->buildProgram();
}

void CLMarkWidget::onIsRunning(bool result) {
    emit isLocked(result);
}
